//
//  HomeTableViewController.m
//  OC-新浪微博
//
//  Created by Apple on 15/11/7.
//  Copyright © 2015年 Apple. All rights reserved.
//

#import "HomeTableViewController.h"
#import "StatusListViewModel.h"
#import "Status.h"
#import "StatusCell.h"
#import "StatusViewModel.h"
@interface HomeTableViewController ()
@property (nonatomic, strong) NSArray *aray;
@property (nonatomic, strong) StatusViewModel *ViewModel;
@end
///可重用cell
NSString *StatusCellNormalId = @"StatusCellNormalId";
@implementation HomeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (![UserAccountViewModel shardViewModel].userlogin) {
        [self.visitorView setUpInfo:nil title:nil];
        return ;
    }
    [self loadData];
   [self prepareTableView];
}
#pragma mark /*******************  准备表格  *******************/
- (void)prepareTableView {
    //注册id
    [self.tableView registerClass:[StatusCell class] forCellReuseIdentifier:StatusCellNormalId];
    // 自动计算行高 - 需要一个自上而下的自动布局的控件，指定一个向下的约束
    self.tableView.estimatedRowHeight = 400;
   
    //取消分割条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}
#pragma mark /*******************  数据源方法  *******************/
//行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.aray.count;
}
//行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   StatusCell *cell = [tableView dequeueReusableCellWithIdentifier:StatusCellNormalId forIndexPath:indexPath];
    
    cell.status = self.aray[indexPath.row];;
    //cell.ViewModel = [[StatusViewModel alloc]init:cell.status];

    //cell.ViewModel.statu = self.aray[indexPath.row];
    return cell;
}
#pragma mark /*******************  代理方法设置行高  *******************/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Status *vm = self.aray[indexPath.row];
    if (vm.rowHeight != 0) {
        return vm.rowHeight;
    }
    StatusCell *cell = [[StatusCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:StatusCellNormalId];
    vm.rowHeight = [cell rowHeight:vm];
    return vm.rowHeight;
}
#pragma mark /*******************  加载数据  *******************/
- (void)loadData {
    [self.ListViewModel loadStatus:^(BOOL isSuccessde, NSArray *array) {
        if (!isSuccessde) {
            [SVProgressHUD showInfoWithStatus:@"您的网络不给力"];
            
            return ;
        }
        self.aray = array;
        
        
    }];
   }
#pragma mark /******************* 懒加载模型   *******************/
- (StatusListViewModel *)ListViewModel {
    if (_ListViewModel == nil) {
        _ListViewModel = [StatusListViewModel new];
    }
    return _ListViewModel;
}
- (void)setAray:(NSArray *)aray {
    _aray = aray;
    [self.tableView reloadData];
}

@end
